/**
 * 下午7:04:09
 */
package com.hsnn.medstgmini.pro.project.controller;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.pro.project.model.BidProjdrugsScore;
import com.hsnn.medstgmini.pro.project.model.BidProjdrugsScoreDetail;
import com.hsnn.medstgmini.pro.project.model.BidProjdrugsScoreDetailLog;
import com.hsnn.medstgmini.pro.project.model.BidProjdrugsScoreLog;
import com.hsnn.medstgmini.pro.project.service.BidProjproddrugManager;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.util.BeanCopierUtils;
import com.hsnn.medstgmini.util.Pagination;
import com.hsnn.medstgmini.util.ParseNumber;

/**
 * @author hsnn
 * zhuanin
 *
 */
@Controller
@RequestMapping("/bidProjproddrug")
public class BidProjproddrugController {
	 private static final Logger log = Logger.getLogger(BidProjproddrugController.class);
	 	
		@Autowired
		private BidProjproddrugManager bidProjproddrugManager;

		@Autowired
		private HttpServletRequest request;
		
		@Autowired
		private HttpSession session;
		
		/**
		 *@category 去项目列表
		 *@author 应晓川
		 *@date 2015年9月8日15:50:41
		 *@return
		 */
		@RequestMapping("/toList")
		public String toList(){
			return "/pro/project/bidProjproddrug/projList";
		}
		
		/**
		 * @category 获取项目列表 【条件：专家所属下的项目】
		 * @author 应晓川
		 * @date 2015年9月8日15:50:37
		 * @return
		 */
		@RequestMapping("/getDetailList")
		@ResponseBody
		public Pagination getDetailList() {
			Pagination page = new Pagination(request);
			SysUser user=(SysUser) request.getSession().getAttribute(Constants.USERINFO);
			try {
				page.getConditions().put("expertId", user.getId());
				page = bidProjproddrugManager.getBidProjectListByExpert(page);
				page.setSuccess(true);
			} catch (Exception e) {
				log.error("",e);
				page.setSuccess(false);
			}
			return page;
		}
		
		/**
		 *@category 到一个项目下去评分药品
		 *@author 应晓川
		 *@date 2015年9月8日15:54:05
		 *@return
		 * @throws UnsupportedEncodingException 
		 */
		@RequestMapping("/toDrugScore")
		public String toDetailList(String projId,String name) throws UnsupportedEncodingException{
			request.setAttribute("projId", projId);
			String projName = new String(name.getBytes("iso8859-1"),"utf-8");
			request.setAttribute("projId", projId);
			request.setAttribute("projName", projName);
			return "/pro/project/bidProjproddrug/drugScore";
		}
		
		
		/**
		 * @category 获取要评分的药品信息
		 * @author 应晓川
		 * @date 2015年9月8日15:50:37
		 * @return
		 */
		@RequestMapping("/getBidProjproddrug")
		@ResponseBody
		public Pagination getBidProjproddrug() {
			Pagination page = new Pagination(request);
			String projId= request.getParameter("projId");
			try {
				page.getConditions().put("projId", projId);
				page = bidProjproddrugManager.getBidProjproddrug(page);
				page.setSuccess(true);
			} catch (Exception e) {
				log.error("",e);
				page.setSuccess(false);
			}
			return page;
		}
		
		/**
		 * @category 获取评分指标
		 * @author 应晓川
		 * @date 2015年9月8日15:50:37
		 * @return
		 */
		@RequestMapping("/getBidScoreRule")
		@ResponseBody
		public Pagination getBidScoreRule() {
			Pagination page = new Pagination(request);
			String projId= request.getParameter("projId");
			try {
				page.getConditions().put("projId", projId);
				page = bidProjproddrugManager.getBidScoreRule(page);
				page.setSuccess(true);
			} catch (Exception e) {
				log.error("",e);
				page.setSuccess(false);
			}
			return page;
		}
		
		/**
		 * @category 提交药品评分分数
		 * @author 应晓川
		 * @date 2015年9月8日15:50:37
		 * @return
		 */
		@RequestMapping("/commitBidScore")
		@ResponseBody
		public Pagination commitBidScore(String ids,String drugId) {
			Pagination page = new Pagination(request);
			String bidProjdrugsScoreId = UUID.randomUUID().toString();
			SysUser user = (SysUser) request.getSession().getAttribute(Constants.USERINFO);
			int sumScore = 0;
			Date now =  new Date();
			try {
				String[] idArray =  ids.split(",");
				for(String idAndScores : idArray){//新增得分详情
					String[] idAndScore = idAndScores.split("_");
					String id = idAndScore[0];
					int score = ParseNumber.toInteger(idAndScore[1]).intValue();
					sumScore+=score;
					BidProjdrugsScoreDetail bidProjdrugsScoreDetail = new BidProjdrugsScoreDetail();//插入
					bidProjdrugsScoreDetail.setBidProjdrugsScoreDetailId(UUID.randomUUID().toString());
					bidProjdrugsScoreDetail.setBidProjdrugsScoreId(bidProjdrugsScoreId);
					bidProjdrugsScoreDetail.setBidEncScoreRuleDetailId(id);
					bidProjdrugsScoreDetail.setScore(score);
					bidProjdrugsScoreDetail.setScoreType(Constants.SCORETYPE_SUBJECTIVE);
					bidProjdrugsScoreDetail.setScoreUser(user.getId());
					bidProjdrugsScoreDetail.setUpdUser(user.getId());
					bidProjdrugsScoreDetail.setUpdDatetime(now);
					if( bidProjproddrugManager.save(bidProjdrugsScoreDetail) ){
						BidProjdrugsScoreDetailLog bidProjdrugsScoreDetailLog = new BidProjdrugsScoreDetailLog();
						BeanCopierUtils.copyProperties(bidProjdrugsScoreDetail, bidProjdrugsScoreDetailLog);
						bidProjdrugsScoreDetailLog.setLogId(UUID.randomUUID().toString());
						if ( bidProjproddrugManager.save(bidProjdrugsScoreDetailLog) ){//保存日志
						}
					}
				}
				
				BidProjdrugsScore bidProjdrugsScoreFormDb = (BidProjdrugsScore) bidProjproddrugManager.getUniquerResult(BidProjdrugsScore.class, "bidProjdrugsId", drugId);
				if(bidProjdrugsScoreFormDb!=null){//更新得分总表
					bidProjdrugsScoreFormDb.setScoreSubject(bidProjdrugsScoreFormDb.getScoreSubject()+sumScore);
					bidProjdrugsScoreFormDb.setUpdDatetime(now);
					bidProjdrugsScoreFormDb.setUpdUser(user.getId());
					bidProjproddrugManager.update(bidProjdrugsScoreFormDb);
				}else{//新增得分总表
					bidProjdrugsScoreFormDb = new BidProjdrugsScore();
					bidProjdrugsScoreFormDb.setBidProjdrugsScoreId(bidProjdrugsScoreId);
					bidProjdrugsScoreFormDb.setBidProjdrugsId(drugId);
					bidProjdrugsScoreFormDb.setScoreSubject(sumScore);
					bidProjdrugsScoreFormDb.setUpdDatetime(now);
					bidProjdrugsScoreFormDb.setUpdUser(user.getId());
					bidProjproddrugManager.save(bidProjdrugsScoreFormDb);
				}
				BidProjdrugsScoreLog bidProjdrugsScoreLog = new BidProjdrugsScoreLog();
				BeanCopierUtils.copyProperties(bidProjdrugsScoreFormDb, bidProjdrugsScoreLog);
				bidProjdrugsScoreLog.setLogId(UUID.randomUUID().toString());
				bidProjproddrugManager.save(bidProjdrugsScoreLog);
				
				page.setSuccess(true);
			} catch (Exception e) {
				log.error("",e);
				page.setSuccess(false);
			}
			return page;
		}
		
}
